package com.kgc.kmall.kmallcartservice.service.impl;

import com.alibaba.fastjson.JSON;
import com.kgc.kmall.bean.OmsCartItem;
import com.kgc.kmall.bean.OmsCartItemExample;
import com.kgc.kmall.kmallcartservice.mapper.OmsCartItemMapper;
import com.kgc.kmall.service.CartService;
import com.kgc.kmall.util.RedisUtil;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Component
public class CartServiceImpl implements CartService {
    @Resource
    OmsCartItemMapper omsCartItemMapper;

    @Resource
    RedisUtil redisUtil;

    @Override
    public OmsCartItem ifCartExistByUser(long  memberId, long skuId) {
        OmsCartItemExample example=new OmsCartItemExample();
        OmsCartItemExample.Criteria criteria = example.createCriteria();
        criteria.andMemberIdEqualTo(memberId);
        criteria.andProductSkuIdEqualTo(skuId);
        List<OmsCartItem> omsCartItems = omsCartItemMapper.selectByExample(example);
        if (omsCartItems!=null&&omsCartItems.size()>0){
            return omsCartItems.get(0);
        }else {
            return null;
        }
    }

    @Override
    public void addCart(OmsCartItem cartItem) {
        omsCartItemMapper.insertSelective(cartItem);
    }

    @Override
    public void updateCart(OmsCartItem cartItem) {
    omsCartItemMapper.updateByPrimaryKeySelective(cartItem);
    }

    @Override
    public void flushCartCache(String memberId) {
        OmsCartItemExample example=new OmsCartItemExample();
        OmsCartItemExample.Criteria criteria = example.createCriteria();
        criteria.andMemberIdEqualTo(Long.parseLong(memberId));
        List<OmsCartItem> omsCartItems = omsCartItemMapper.selectByExample(example);

        // 同步到redis缓存中
        Jedis jedis = redisUtil.getJedis();
        Map<String,String> map = new HashMap<>();
        for (OmsCartItem cartItem : omsCartItems) {
            map.put(cartItem.getProductSkuId().toString(), JSON.toJSONString(cartItem));
        }

        jedis.del("user:"+memberId+":cart");
        jedis.hmset("user:"+memberId+":cart",map);

        jedis.close();
    }

    @Override
    public List<OmsCartItem> cartList(String memberId) {
      OmsCartItemExample example=new OmsCartItemExample();
      example.createCriteria().andMemberIdEqualTo(Long.parseLong(memberId));
        List<OmsCartItem> cartItems = omsCartItemMapper.selectByExample(example);
        return cartItems;
    }

    @Override
    public void checkCart(OmsCartItem omsCartItem) {
        /*omsCartItemMapper.updateByPrimaryKeySelective(omsCartItem);

        //刷新缓存
        flushCartCache(omsCartItem.getMemberId().toString());*/

        OmsCartItemExample example=new OmsCartItemExample();
        OmsCartItemExample.Criteria criteria = example.createCriteria();
        criteria.andMemberIdEqualTo(omsCartItem.getMemberId());
        criteria.andProductSkuIdEqualTo(omsCartItem.getProductSkuId());
        omsCartItemMapper.updateByExampleSelective(omsCartItem,example);

        flushCartCache(omsCartItem.getMemberId().toString());
    }
}


